<?php
namespace Home\Controller;
use Think\Controller;

  
 
class MoneyController extends Controller {
    function __construct(){
        parent::__construct();
		
			if (!CheckLogin()) {
		
			//$this->redirect('Home/Login/index', array() , 0, '未登陆，跳转到登陆界面...');
		}       
    }

	//金币流水
	public function index(){
	
			$UserId = I('session.menuid');
			
			$default_begin_time = date("Y-m-d",strtotime("-3 Days")); 
			$default_end_time =date("Y-m-d",time());
			$startTime = I('get.start_date',$default_begin_time);		
			$preSevenDays = strtotime("-7 Days");
			
			if(strtotime($startTime)<$preSevenDays){
				$startTime = date("Y-m-d", $preSevenDays); 
			}			
			$endTime = I('get.end_date',$default_end_time);			
			if(strtotime($endTime)>time()){
				$endTime = $default_end_time; 
			}
			$money = M('tbuseraccount')->field('nmoney,ngiftmoney')->where("nuserid=$UserId")->find();
			
			if(empty($money['nmoney'])){
				$money['nmoney']=0;
			}			
			if(empty($money['ngiftmoney'])){
				$money['ngiftmoney']=0;
			}
			$this->assign('money',$money['nmoney']);
			$this->assign('giftmoney',$money['ngiftmoney']);	
			$condition['nuserid']=$UserId;
			$condition['dtime'] = array('BETWEEN',array($startTime,date("Y-m-d", strtotime("$endTime+1 days"))));
			$G = M('tbusermoneyflow');
			$count =$G->where($condition)
					  ->count();
			$items =15;
			$Page = new \Think\Userpage($count,$items);
					
			$data = $G->field('dtime,cbuddy,nbuddyid,nmoney,nbalance,cdesc')
					  ->where($condition)
					  ->order('dtime DESC')
					  ->limit($Page->firstRow.','.$Page->listRows)	
					  ->select();
			$listshow = $Page->show(); 
			$this->assign('list',$listshow);
			$this->assign('data',$data);
			
			$this->assign('startTime',I('get.start_date',$default_begin_time));			
			$this->assign('endTime',I('get.end_date',$default_end_time));
			

			$thisMonth = date('Y-m-01');  //本月日期
			$lastMonth = date('Y-m-01',strtotime("last month")); //上月日期
			$M= M('tbuserconsumemonth');
			//上月消费、上月排名
			$Last = $M->field('nmoney')->where("nuserid=$UserId and ddate='$lastMonth'")->select();
			if(!empty($Last)){
				$LspendMoney = $Last[0]['nmoney'];
				$LastRank = $M->where("nmoney>$LspendMoney and ddate='$lastMonth' ")->count();
				$this->assign('LspendMoney',$LspendMoney);
				$this->assign('Lrank',$LastRank+1);

			}else{
				$this->assign('LspendMoney',0);
				$this->assign('Lrank',0);
			}
			
			//本月消费、本月排名
			$This= $M->field('nmoney')->where("nuserid=$UserId and ddate='$thisMonth'")->select();
			if(!empty($This)){
				$TspendMoney = $This[0]['nmoney'];
				$ThisRank = $M->where("nmoney>$TspendMoney and ddate='$thisMonth'")->count();
				$Hrank = $M->field('nmoney')->where("nmoney>$TspendMoney and ddate='$thisMonth'")->order('nmoney asc')->limit(1)->select();
				$lessMoney = $Hrank[0]['nmoney']-$TspendMoney ;
				$this->assign('TspendMoney',$TspendMoney);
				$this->assign('Trank',$ThisRank+1);	
				$this->assign('lessMoney',$lessMoney);	
			}else{
				$this->assign('TspendMoney',0);
				$this->assign('Trank',0);
				$this->assign('lessMoney',0);
			}

			
			
			
			$this->display();
		
		}

	//划币管理
	public function spendMoney(){
	
			$UserId = I('session.menuid');
			//是否有划币权限
			$flag = 0;
			$ifHua = M('tbuseraccount')->field('nolevel')->where("nuserid=$UserId")->find();
			if($ifHua['nolevel'] ==7){
				$flag =1;
			}
			$this->assign('ifhua',$flag);
			$default_begin_time = date("Y-m-d",strtotime("-3 Days")); 
			$default_end_time =date("Y-m-d",time());
			$startTime = I('get.start_date',$default_begin_time);		
			$preSevenDays = strtotime("-7 Days");
			
			if(strtotime($startTime)<$preSevenDays){
				$startTime = date("Y-m-d", $preSevenDays); 
			}			
			$endTime = I('get.end_date',$default_end_time);			
			if(strtotime($endTime)>time()){
				$endTime = $default_end_time; 
			}
						
			
			$condition['nuserid']=$UserId;
			$condition['ntype']=3;
			$condition['dtime'] = array('BETWEEN',array($startTime,date("Y-m-d", strtotime("$endTime+1 days"))));
			$G = M('tbusermoneyflow');
			$count =$G->where($condition)
					  ->count();
			$items =15;
			$Page = new \Think\Userpage($count,$items);
					
			$data = $G->field('dtime,cbuddy,nbuddyid,nmoney,nbalance,cdesc')
					  ->where($condition)
					  ->order('dtime DESC')
					  ->limit($Page->firstRow.','.$Page->listRows)	
					  ->select();
					  
			$listshow = $Page->show(); 
			$this->assign('list',$listshow);
			$this->assign('data',$data);			
			
			$this->assign('startTime',I('get.start_date',$default_begin_time));			
			$this->assign('endTime',I('get.end_date',$default_end_time));	

			$money = M('tbuseraccount')->field('nmoney')->where("nuserid=$UserId")->select();
			$this->assign('money',$money[0]['nmoney']);			
			$this->display();
		
		}	
	//我的保险箱
	public function mybox(){
	
			$UserId = I('session.menuid');
			$default_begin_time = date("Y-m-d",strtotime("-3 Days")); 
			$default_end_time =date("Y-m-d",time());
			$startTime = I('get.start_date',$default_begin_time);		
			$preSevenDays = strtotime("-7 Days");
			
			if(strtotime($startTime)<$preSevenDays){
				$startTime = date("Y-m-d", $preSevenDays); 
			}			
			$endTime = I('get.end_date',$default_end_time);			
			if(strtotime($endTime)>time()){
				$endTime = $default_end_time; 
			}
			$this->assign('startTime',I('get.start_date',$default_begin_time));			
			$this->assign('endTime',I('get.end_date',$default_end_time));		
			$condition['nuserid']=$UserId;
			$condition['dtime'] = array('BETWEEN',array($startTime,date("Y-m-d", strtotime("$endTime+1 days"))));
			$G = M('tbusersafemoneyflow');
			$count =$G->where($condition)
					  ->count();
			$items =15;
			$Page = new \Think\Userpage($count,$items);
					
			$data = $G->field('dtime,cbuddy,nbuddyid,nmoney,nbalance,cdesc')
					  ->where($condition)
					  ->order('dtime DESC')
					  ->limit($Page->firstRow.','.$Page->listRows)	
					  ->select();
			$listshow = $Page->show(); 
			$this->assign('list',$listshow);
			$this->assign('data',$data);	
			$money = M('tbuseraccount')->field('nmoney,nsafemoney')->where("nuserid=$UserId")->find();
			$this->assign('money',$money['nmoney']);	
			$this->assign('safemoney',$money['nsafemoney']);				
			$this->display();
		
		}

	//划币页面
	public function transferMoney(){
		$UserId = I('session.menuid');
		$money = M('tbuseraccount')->field('nmoney')->where("nuserid=$UserId")->find();
		$this->assign('money',$money['nmoney']);	
		$this->display();
		
	
	}

	
	//礼金兑换页面
	public function giftExchange(){
		$UserId = I('session.menuid');
		if($UserId ){
			$money = M('tbuseraccount')->field('nmoney,ngiftmoney')->where("nuserid=$UserId")->find();
			if(empty($money['nmoney'])){
				$money['nmoney']=0;
			}			
			if(empty($money['ngiftmoney'])){
				$money['ngiftmoney']=0;
			}
			$this->assign('money',$money['nmoney']);
			$this->assign('giftmoney',$money['ngiftmoney']);
		}
		$this->display();			
	}	
	
	//从保险箱取出页面
	public function outbox(){
		$UserId = I('session.menuid');
		if($UserId ){		
			$money = M('tbuseraccount')->field('nmoney,nsafemoney')->where("nuserid=$UserId")->find();	
			if(empty($money['nmoney'])){				
				$money['nmoney'];
			}
			if(empty($money['nsafemoney'])){				
				$money['nsafemoney'];
			}			
			$this->assign('money',$money['nmoney']);	
			$this->assign('safemoney',$money['nsafemoney']);	
		}
		$this->display();		
	
	}	

	//存入保险箱页面
	public function inbox(){
		$UserId = I('session.menuid');
		if($UserId ){
			$money = M('tbuseraccount')->field('nmoney,nsafemoney')->where("nuserid=$UserId")->find();
			$this->assign('money',$money['nmoney']);	
			$this->assign('safemoney',$money['nsafemoney']);	
		}		
		$this->display();	
	
	}	



	Public function oper(){
	
		if($_POST){
			$UserId = I('session.menuid');
			if($UserId ){	
			if(I('post.from')==1){
					$pw = I('post.psw','');
					$psw =CreatePassword($pw);					
					$userInfor = M('tbuserinformation')->field('cpassword')->where("nuserid=$UserId")->select();	
					if(empty($pw)){
					   echo 1;
					}else if($psw!=$userInfor[0]['cpassword']){
					  echo 2;
					}else{
						echo 3;
					}		
				}else if(I('post.from')==2){
					$inid = I('post.inid');   //划入账号				
					$C = M('tbuseraccount');						
					$checkIn = $C->where("nuserid=$inid")->count();
					if(empty($checkIn)){
						echo 1;
						return;
					}else{
						$inName =  M('tbuserinformation')->field('calias')->where("nuserid=$inid")->find();
						echo $inName['calias'];
					}
				}else if(I('post.from')==6){
						$C = M('tbuseraccount');
						$inid = I('post.inid');   //划入账号
						$intotal = I('post.intotal');  //划入金额
						$intotal= (int)$intotal;
						$receivetotal = $intotal;   //划入账号金额
						$tip ="用户间划币";					
						//划入帐号的昵称
						$checkLevel = $C->field('nviplevel')->where("nuserid=$UserId")->select();
						if(empty($checkLevel[0]['nviplevel'])){
						
							$level = $C->field('nmoney')->where("nuserid=$UserId")->select();							
							$tot = $level[0]['nmoney'];																					
							$intotal=$intotal*1.1;  //如果是普通用户，扣除手续费							
							$tip ="用户间划币(包含10%手续费)";
							if($tot<$intotal){
								echo 4;
								return;
							}
						}					
						//开启事务
						$C->startTrans();
						$i = $C->where("nuserid=$UserId")->setDec('nmoney',$intotal);			
						$k = $C->where("nuserid=$inid")->setInc('nmoney',$receivetotal);	
						if(!empty($i)&&!empty($k)){
						//操作者划币记录
							$userMoney = $C->field('nmoney')->where("nuserid=$UserId")->find();
							$userMoney = $userMoney['nmoney'];
							$data['ntype']=3;
							$data['nmoney']= -$intotal;
							$data['dtime']=date('Y-m-d H:i:s');
							$data['nuserid'] = $UserId;
							$data['nbuddyid'] = $inid;
							$data['nbalance'] = $userMoney;	
							$data['cdesc'] = $tip;
						
							$r=M('tbusermoneyflow')->add($data);
						//接收者划币记录
							$receiverMoney = $C->field('nmoney')->where("nuserid=$inid")->find();
							$receiverMoney = $receiverMoney['nmoney'];
							$infor['ntype']=3;				
							$infor['nmoney']= $receivetotal;
							$infor['dtime']=date('Y-m-d H:i:s');
							$infor['nuserid'] = $inid;
							$infor['cbuddy'] = $UserId;
							$infor['nbalance'] = $receiverMoney;
							$infor['cdesc'] = "用户间划币"; 
							$j=M('tbusermoneyflow')->add($infor);
						
							if($r && $j){
								$C->commit();
								echo 2;
							}														
						}else{
							$C->rollback();
							echo 3;
						}
				}else if(I('post.from')==3){
						$money =I('post.money');
						$C = M('tbuseraccount');
						$C->startTrans();
						$i=$C->where("nuserid=$UserId")->setInc('nmoney',$money);
						$k=$C->where("nuserid=$UserId")->setDec('ngiftmoney',$money);		
						if(!empty($i)&&!empty($k)){	
							$userMoney = $C ->field('nmoney,ngiftmoney')->where("nuserid=$UserId")->find();
							$goleden = $userMoney['nmoney'];
							$gift = $userMoney['ngiftmoney'];
							$data['nmoney']= -$money;
							$data['ntype']=2;
							$data['dtime']=date('Y-m-d H:i:s');
							$data['nuserid'] = $UserId;
							$data['nbalance'] = $gift;
							$data['cdesc'] = "礼金兑换"; 
							$r=M('tbusergiftmoneyflow')->add($data);							
							$data['nmoney']= $money;
							$data['nbalance']= $goleden;							
							$j=M('tbusermoneyflow')->add($data);														
							if($r&&$j){
								$C->commit();
								echo 1;
							}
						}else{
							$C->rollback();
							echo 2; 
						}
				}else if(I('post.from')==4){
					$pw = I('post.psw','');
					$psw =CreatePassword($pw);				
					$userInfor = M('tbuserinformation')->field('cpassword')->where("nuserid=$UserId")->find();	
					
					if($psw!=$userInfor['cpassword']){
					   echo 1;	
					   return;
					}else{
						$money =I('post.total');
						$C = M('tbuseraccount');
						$C->startTrans();
						$i=$C->where("nuserid=$UserId")->setInc('nsafemoney',$money);
						$k=$C->where("nuserid=$UserId")->setDec('nmoney',$money);	
						if(!empty($i)&&!empty($k)){	
						
							$userMoney = $C ->field('nsafemoney,nmoney')->where("nuserid=$UserId")->find();
							$safemoney = $userMoney['nsafemoney'];
							$glodenmoney = $userMoney['nmoney'];
							$data['ntype']=1;
							$data['nmoney']= $money;
							$data['dtime']=date('Y-m-d H:i:s');
							$data['nuserid'] = $UserId;
							$data['nbalance'] = $safemoney;
							$data['cdesc'] = "存入保险箱"; 
							$r=M('tbusersafemoneyflow')->add($data);
							$data['nmoney']= -$money;
							$data['nbalance'] = $glodenmoney;
							$j=M('tbusermoneyflow')->add($data);
							if($r&&$j){
								$C->commit();
								echo 2;
							}
						}else{
							$C->rollback();
							echo 3;
						}
					}											
				}else if(I('post.from')==5){
					$pw = I('post.psw','');
					$psw =CreatePassword($pw);				
					$userInfor = M('tbuserinformation')->field('cpassword')->where("nuserid=$UserId")->find();	
					
					if($psw!=$userInfor['cpassword']){
					   echo 1;	
					   return;
					}else{
						$money =I('post.total');
						$C = M('tbuseraccount');
						$C->startTrans();
						$i=$C->where("nuserid=$UserId")->setInc('nmoney',$money);
						$k=$C->where("nuserid=$UserId")->setDec('nsafemoney',$money);	
						if(!empty($i)&&!empty($k)){	
							$userMoney = $C ->field('nsafemoney,nmoney')->where("nuserid=$UserId")->find();
							$safemoney = $userMoney['nsafemoney'];
							$glodenmoney = $userMoney['nmoney'];
							$data['ntype']=1;
							$data['nmoney']= -$money;
							$data['dtime']=date('Y-m-d H:i:s');
							$data['nuserid'] = $UserId;
							$data['nbalance'] = $safemoney;
							$data['cdesc'] = "从保险箱取出"; 
							$r=M('tbusersafemoneyflow')->add($data);
							$data['nmoney']= $money;
							$data['nbalance'] = $glodenmoney;
							$j=M('tbusermoneyflow')->add($data);
							if($r&&$j){
								$C->commit();
								echo 2;
							}						
						}else{
							$C->rollback();
							echo 3;
							}
						}				
				
					}
				}
			}			
		}				
}
